package com.ruoyi.web.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliyunOSSConfig {

    private String endpoint ;

    private String accessKeyId ;

    private String accessKeySecret;

    private String dir ;

    private String bucket;

    private boolean use;

    @Bean(destroyMethod = "shutdown")
    public OSS oSSClient() {
        OSS ossClient= new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        if(use){
            /* 通过一个Bucket对象来创建 */
            CreateBucketRequest bucketObj = new CreateBucketRequest(null);// 构造函数入参为Bucket名称，可以为空
            bucketObj.setBucketName(bucket);// 设置bucketObj名称
            bucketObj.setCannedACL(CannedAccessControlList.Private);// 设置bucketObj访问权限acl
            ossClient.createBucket(bucketObj);// 创建Bucket
        }
        return ossClient;
    }
}
